package main

import (
	"sync"
	"sync/atomic"
	"testing"
)

/*
运行基准测试： go test -bench=. -benchmem
*/

func BenchmarkAtomicCounter(b *testing.B) {
	var count int64
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			atomic.AddInt64(&count, 1)
		}
	})
}

func BenchmarkMutexCounter(b *testing.B) {
	var (
		count int64
		mu    sync.Mutex
	)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			mu.Lock()
			count++
			mu.Unlock()
		}
	})
}
